﻿using Youz.Common.BLL;
using Youz.Common.Entity;
using Youz.Common.Helper;
using Youz.CreditRepay.BLL;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Json;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using FluentScheduler;

namespace Youz.CreditRepay.Server
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            var serializer = JsonSerializer.Create(new JsonSerializerSettings
            {
                DateTimeZoneHandling = DateTimeZoneHandling.Local
                //DateFormatHandling = DateFormatHandling.IsoDateFormat,
                //NullValueHandling = NullValueHandling.Ignore
            });

            GlobalHost.DependencyResolver.Register(typeof(JsonSerializer), () => serializer);
            GlobalHost.Configuration.DisconnectTimeout = new TimeSpan(0, 0, 120);
            
            JobManager.Initialize(new Tasker());
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            string strError = Lang_LanguageBLL.LanguageByWebSite(LanguageModule.Log_ErrorLog, "GlobalErrorMessage", "系统繁忙，请稍后重试...");

            Exception ex = HttpContext.Current.Server.GetLastError();

            if (ex is HttpRequestValidationException)
            {
                strError = Lang_LanguageBLL.LanguageByWebSite(LanguageModule.Log_ErrorLog, "HttpRequestValidationException", "系統檢測到您提交的數據中含有特殊字符，請修改後再重新提交！");
            }

            try
            {
                Core.WriteErrorLog(ex);
            }
            catch { }

#if DEBUG

#else
            //针对Ajax报错的处理；
            if (CommonHelper.IsAjaxRequest())
            {
                Response.Write(Core.AjaxResult(false, strError));
                Response.End();
            }
#endif
        }
    }
}